if SERVER then

	AddCSLuaFile( "shared.lua" )
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
	SWEP.HoldType			= "slam"

end

if CLIENT then

	SWEP.DrawAmmo			= false
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 2
	SWEP.ViewModelFlip		= false
	SWEP.CSMuzzleFlashes	= false
	
	SWEP.IconFont = "CSSelectIcons"
	
	SWEP.PrintName = "Inventory"
	SWEP.Slot = 0
	SWEP.Slotpos = 0
	SWEP.IconLetter = "H"
	
	function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
		draw.SimpleText( self.IconLetter, self.IconFont, x + wide/2, y + tall*0.2, Color( 15, 20, 200, 255 ), TEXT_ALIGN_CENTER )
	end

end

 SWEP.Spawnable = true;
 SWEP.AdminSpawnable = true;

 SWEP.ViewModel	= ""
 SWEP.WorldModel = "models/weapons/w_package.mdl" 

 SWEP.Primary.ClipSize              = 0
 SWEP.Primary.DefaultClip           = -1
 SWEP.Primary.Automatic             = false
 SWEP.Primary.Ammo                  = "none"

 SWEP.Secondary.Clipsize            = -1;
 SWEP.Secondary.DefaultClip         = -1;
 SWEP.Secondary.Ammo                = "none"; 
 SWEP.Secondary.Delay			= 4
 
 SWEP.InvSelect = Sound("weapons/c4/c4_disarm.wav")
 SWEP.InvFuncMoveSound = Sound("buttons/lightswitch2.wav")
 SWEP.InvMoveSound = Sound("common/wpn_moveselect.wav")
 
function SWEP:GetUsedAmmoType()
	return "none"
end

function SWEP:GetWorldModel()
	return "none"
end

function SWEP:GetMaxAmmo()
	return self.Primary.ClipSize or 1
end

function SWEP:Initialize()
    if (SERVER) then
		self:SetWeaponHoldType(self.HoldType)
	end
end 

function SWEP:CanPrimaryAttack()
	return CurTime() > self.NextPrimary
end

function SWEP:CanSecondaryAttack()
	return CurTime() > self.NextSecondary
end
 
function SWEP:Deploy()
	self.InvTbl = nil
	self.InvItem = nil
	self.FuncTbl = {}
	if CLIENT then return end
	
	self.Owner:SetNetworkedString("Mode", "selection")
	self:SetFuncPos(1)
	self:SetInvPos(1)
	
	self.Owner:DrawViewModel( false )
	self.Owner:SynchInventory()
end

function SWEP:RefreshIcons()
	if self.cProps then
		for k,v in pairs(self.cProps) do
			if v:IsValid() then
				v:Remove()
			end
		end
		self.cProps = nil
	end
end

function SWEP:SetFuncPos(num)
	if SERVER then
		umsg.Start("cSetFuncPos", self.Owner)
		umsg.Short(num or 0)
		umsg.End()
	end
	self.FuncPos = num
end	

function SWEP:SetInvPos(num)
	if SERVER then
		umsg.Start("cSetInvPos", self.Owner)
		umsg.Short(num or 0)
		umsg.End()
	end
	self.InvPos = num
end	

function SWEP:SetMode(mode)
	self.Owner:SetNetworkedString("Mode", mode)
end
 
SWEP.NextPrimary = 0
function SWEP:PrimaryAttack()
	if not self.Owner.InveHint then
		self.Owner.InveHint = true
		Inventory.Notice(self.Owner, "Secondary fire changes your position", Inventory.White)
		Inventory.Notice(self.Owner, "Primary fire selects an item", Inventory.White)
	end
	if not self:CanPrimaryAttack() then return end
	self.NextPrimary = CurTime() + .15
	if SERVER then
		if self.Owner:GetInventory()[1] == nil then return end
	elseif CLIENT then
		if GameInfo.cInv[1] == nil then return end
	end
	if self.Owner:GetNetworkedString("Mode", "functions") == "selection" then
		self:SetMode("functions")
		self:SetFuncPos(1)
		if SERVER then
			self.Owner:EmitSound(self.InvSelect,50,math.random(95,100))
			local inv = self.Owner:GetInventory()
			local order = self:FindInvOrder(self.InvPos or 1,table.Count(inv))
			if string.find(inv[order[1]],"weapons/") then
				self.FuncTbl = {Inventory.DropWeapon}
			else
				self.FuncTbl = {Inventory.Drop,Inventory.Give}
				table.Add(self.FuncTbl,Inventory.FindTable(inv[order[1]]).Functions)
			end
		end
	elseif self.Owner:GetNetworkedString("Mode", "functions") == "functions" then
		self:SetMode("selection")
		if SERVER then	
			local inv = self.Owner:GetInventory()
			local order = self:FindInvOrder(self.InvPos,table.Count(inv))
			self.FuncTbl[self.FuncPos](self.Owner,inv[order[1]])
			self:SetInvPos(1)
		end
	end
end

SWEP.NextSecondary = 0
function SWEP:SecondaryAttack()	
	if not self:CanSecondaryAttack() then return end
	self.NextSecondary = CurTime() + .15
	if SERVER then
		if self.Owner:GetInventory()[1] == nil then return end
	else 
		if GameInfo.cInv[1] == nil then return end
	end
	if self.Owner:GetNetworkedString("Mode","functions") == "selection" then
		if SERVER then
			self:SetInvPos((self.InvPos or 0) + 1)
			if self.InvPos > table.Count(self.Owner:GetInventory()) then 
				self:SetInvPos(1)
			end
			return
		elseif CLIENT then
			self.Owner:EmitSound(self.InvMoveSound,50,math.random(95,100))
			return
		end
	elseif self.Owner:GetNetworkedString("Mode","functions") == "functions" then
		if SERVER then
			self:SetFuncPos((self.FuncPos or 0) + 1)
			if self.FuncPos > table.Count(self.FuncTbl) then 
				self:SetFuncPos(1)
			end
			return
		elseif CLIENT then
			self.Owner:EmitSound(self.InvFuncMoveSound,50,math.random(95,100))
			return
		end
	end
end

function SWEP:FindInvOrder(pos,size) //returns the tbl pos of 5 items
	local start = pos
	local order = {}
	for i=1,math.Clamp(size,0,5) do
		pos = pos + 1 
		if pos > size then
			pos = 1
		end
		order[i] = pos
		if pos == start then return order end
	end
	return order
end

if (CLIENT) then
	surface.CreateFont("Typenoksidi", 12, 500, true, false, "InvText")
end

InvBox = {}
InvBox.Black = Color(40, 40, 40, 200)
InvBox.Black2 = Color(60, 60, 60, 200)
InvBox.Border = .01
InvBox.Border2x = .02
InvBox.Width = .06
InvBox.Height = InvBox.Width + .02
InvBox.StartY = .90
InvBox.StartX = .92

FuncBox = {}
FuncBox.Black = table.Copy(InvBox.Black)
FuncBox.Width = .08
FuncBox.Height = InvBox.Height + .02
FuncBox.StartX = .82
FuncBox.StartY = .89

PropPos = {.90, .75, .66, .57, .48}

function SWEP:DrawHUD()

	if not ToggleHUD then return end

	local w,h = ScrW(),ScrH()
	local drawcol = Color(220,220,220,200)
	
	surface.SetDrawColor(70, 80, 220, 220)
	local wh, lh, sh = w*.5, h*.5, 10
	surface.DrawLine(wh - sh, lh - sh, wh + sh, lh - sh) //top line
	surface.DrawLine(wh - sh, lh + sh, wh + sh, lh + sh) //bottom line
	surface.DrawLine(wh - sh, lh - sh, wh - sh, lh + sh) //left line
	surface.DrawLine(wh + sh, lh - sh, wh + sh, lh + sh) //right line
	
	//left borderwall
	draw.RoundedBox(0, w*(InvBox.StartX-InvBox.Border+.001), h*(InvBox.StartY-InvBox.Border), w*InvBox.Border, h*(InvBox.Height+InvBox.Border2x-.001), InvBox.Black ) 
	//right borderwall
	draw.RoundedBox(0, w*(InvBox.StartX+InvBox.Width), h*(InvBox.StartY-InvBox.Border), w*InvBox.Border, h*(InvBox.Height+InvBox.Border2x-.001), InvBox.Black ) 
	//top borderwall
	draw.RoundedBox(0, w*(InvBox.StartX+.001), h*(InvBox.StartY-InvBox.Border), w*(InvBox.Width-.001), h*InvBox.Border, InvBox.Black ) 
	//bottom borderwall
	draw.RoundedBox(0, w*(InvBox.StartX+.001), h*(InvBox.StartY+InvBox.Height-.001), w*(InvBox.Width-.001), h*InvBox.Border, InvBox.Black ) 
	
	//info box
	draw.RoundedBox(0, w*(InvBox.StartX-InvBox.Border+.001), h*(InvBox.StartY-.05), w*(InvBox.Width+.02), h*.03, InvBox.Black ) 
	
	//name
	if GameInfo.InvTbl then
		draw.SimpleText(GameInfo.InvTbl.Name, "HudInfo", w*(InvBox.StartX+.03), h*(InvBox.StartY-.045), Color(250,250,250,250),1)
	elseif GameInfo.cInv[1] == nil then
		draw.SimpleText("No Items", "HudInfo", w*(InvBox.StartX+.03), h*(InvBox.StartY-.045), Color(250,250,250,250),1)
	end
	
	//generate functions list
	if LocalPlayer():GetNetworkedString("Mode","functions") == "functions" then
		draw.RoundedBox(0, w*FuncBox.StartX, h*FuncBox.StartY, w*FuncBox.Width, h*FuncBox.Height, FuncBox.Black ) 
		if GameInfo.InvTbl then
			local ypos = FuncBox.StartY + .01
			for k,v in pairs(GameInfo.InvTbl.Functions) do
				local text = GameInfo.InvTbl.Functions[k](nil,nil,true) or "DICKS"
				if FuncPos == k then
					draw.SimpleText(text, "HudInfo", w*(FuncBox.StartX+.01), h*ypos, Color(50,250,60,250),0)
				else
					draw.SimpleText(text, "HudInfo", w*(FuncBox.StartX+.01), h*ypos, Color(250,250,250,250),0)
				end
				ypos = ypos + .02
			end
		end
	end
end

function SWEP:Reload()
	self.Owner:SetNetworkedString("Mode", "selection")
	self:SetInvPos(1)
	self:SetFuncPos(1)
end

SWEP.Refresh = nil
function SWEP:Think()
	if CLIENT then
		if LocalPlayer():KeyDown( IN_USE ) and not self.Refresh then
			self.Refresh = CurTime() + .2
			self:SetFuncPos(1)
		end
		if self.Refresh then
			if self.Refresh < CurTime() then
				self.Refresh = nil
				//self:RefreshIcons()
			end
		end
	else
		if self.Owner:KeyDown( IN_USE ) then
			self:SetFuncPos(1)
		end
	end
end

function SWEP:OnRemove()
	//self:RefreshIcons()
end

function SWEP:OwnerChanged()
	//self:RefreshIcons()
end

function SWEP:Holster()
	if SERVER then return true end
	//self:RefreshIcons()
end
